package com.app.controller;

import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.UnitsManagerModel;
import com.app.server.AdminUserService;
import com.app.server.UnitsManagerService;
import com.app.utils.CommonHelp;
import com.app.utils.RandomUnits;
import com.app.vo.UnitsManagerVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static org.apache.commons.codec.digest.DigestUtils.md5Hex;

/**
 * 单位管理
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/UnitsManager")
public class UnitsManagerController {
    @Autowired
    private UnitsManagerService unitsManagerService;
    @Autowired
    private AdminUserService adminUserService;

    // 查询列表
    @GetMapping("/getUnitsManagerList")
    public Result getUnitsManagerList(@RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "50") Integer limit, String keyword, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        UnitsManagerVo unitsManagerList = unitsManagerService.getUnitsManagerList(page, limit,keyword);
        return Result.success(unitsManagerList);
    }

    //根据id查询
    @GetMapping("/getUnitsManagerById")
    public Result getUnitsManagerById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        UnitsManagerModel unitsManager = unitsManagerService.getById(id);
        return Result.success(unitsManager);
    }

    // 新增和修改
    @PostMapping("/addAndUpdateUnits")
    public Result addAndUpdateUnitsManager(@RequestBody UnitsManagerModel unitsManagerModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        UnitsManagerModel unitsManager = unitsManagerService.getById(unitsManagerModel.getId());
        if (unitsManager != null) {
            //单位管理信息修改
            unitsManagerModel.setUpdateTime(CommonHelp.getNowTimestamp());
            unitsManagerService.updateById(unitsManagerModel);
            //管理员表中单位管理员修改
            QueryWrapper<AdminUserModel>qw =new QueryWrapper<>();
            qw.eq("units_id",unitsManagerModel.getId());
            AdminUserModel adminUser = adminUserService.getOne(qw);
            adminUser.setRealName(unitsManagerModel.getName());
            adminUserService.updateById(adminUser);
            return Result.success("修改成功");
        } else {
            unitsManagerModel.setCreateTime(CommonHelp.getNowTimestamp());
            unitsManagerModel.setRoleName("单位管理员");
            unitsManagerService.save(unitsManagerModel);
            //添加单位管理时默认添加单位管理账号
            UnitsManagerModel addUnitsManager = null;
            try {
                addUnitsManager = unitsManagerService.getByName(unitsManagerModel.getName());
            } catch (Exception e) {
                return Result.success("单位名称重复");
            }
            adminUserService.addUnitsManager(addUnitsManager);
            return Result.success("添加成功");
        }
    }

    //删除
    @GetMapping("/delUnitsManager")
    public Result delUnitsManager(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        //单位管理假删
        UnitsManagerModel unitsManager =new UnitsManagerModel();
        unitsManager.setId(id);
        unitsManager.setDel(1);
        unitsManager.setUpdateTime(CommonHelp.getNowTimestamp());
        unitsManagerService.updateById(unitsManager);
//        unitsManagerService.removeById(id);
        //管理员账号表 单位管理账号真删
        QueryWrapper<AdminUserModel>qw =new QueryWrapper<>();
        qw.eq("units_id",id);
        AdminUserModel adminUser = adminUserService.getOne(qw);
        adminUserService.removeById(adminUser.getId());
        return Result.success("删除成功");
    }
}
